<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="周期名称" prop="cycleName">
                <el-select
                        v-model="queryParams.cycleName"
                        placeholder="请选择周期"
                        clearable
                        size="small"
                        style="width: 240px"
                >
                    <el-option
                            v-for="dict in annualPlanList"
                            :key="dict.id"
                            :label="dict.cycleName"
                            :value="dict.cycleName"
                    />
                </el-select>
            </el-form-item>-->

      <el-form-item label="周型" prop="type" :formatter="typeFormat">
        <el-select v-model="queryParams.type" placeholder="请选择周型" clearable size="small" style="width: 240px">
          <el-option v-for="dict in typeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['training:trainingWeek:add']"
                >新增
                </el-button>
            </el-col>-->
      <!-- <el-col :span="1.5">
                <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['training:trainingWeek:remove']"
                >删除
                </el-button>
            </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="trainingWeekList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="周期" align="center" prop="cycleName" />
      <!--      <el-table-column label="周名称" align="center" prop="weekName" />-->
      <el-table-column label="周型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="当前周时间区间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span> 至
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--            <el-table-column label="强度" align="center" prop="strength"/>-->
      <el-table-column label="训练地点" align="center" prop="trainingLocationName" />
      <el-table-column label="领队" align="center" prop="teamLeaderName" />
      <el-table-column label="主教练" align="center" prop="headCoachName" />
      <el-table-column label="专项教练" align="center" prop="specialCoachName" />
      <el-table-column label="体能教练" align="center" prop="fitnessCoachName" />
      <el-table-column label="队医" align="center" prop="teamDoctorName" />
      <!--            <el-table-column label="周计划描述" align="center" prop="outline"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['training:trainingWeek:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['training:trainingWeek:remove']">删除
          </el-button>
          <el-button size="mini" type="text">
            <router-link :to="'/plan/trainingDay?weekId=' + scope.row.id" class="link-type">
              <span>训练日</span>
            </router-link>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改trainingWeek对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-scrollbar :wrapStyle="'overflow-x:hidden;'">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <div class="form-item">
            <div class="item-cont">
              <el-form-item label="周期名称" prop="annualPlanId">
                <el-select v-model="form.annualPlanId" placeholder="请选择周期" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in annualPlanList" :key="dict.id" :label="dict.cycleName" :value="dict.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="周型" prop="type" :formatter="typeFormat">
                <el-select v-model="form.type" placeholder="请选择周型" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in typeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                </el-select>
              </el-form-item>
              <el-form-item label="训练地点" prop="trainingLocationId">
                <el-select v-model="form.trainingLocationId" placeholder="请输入训练地点" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in trainingLocationList" :key="dict.id" :label="dict.venue" :value="dict.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="领队" prop="teamLeader">
                <el-select v-model="form.teamLeader" placeholder="请选择领队" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in sysUserList" :key="dict.userId" :label="dict.userName" :value="dict.userId" />
                </el-select>
              </el-form-item>

              <el-form-item label="主教练" prop="headCoach">
                <el-select v-model="form.headCoach" placeholder="请选择主教练" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in sysUserList" :key="dict.userId" :label="dict.userName" :value="dict.userId" />
                </el-select>
              </el-form-item>
              <el-form-item label="专项教练" prop="specialCoach">
                <el-select v-model="form.specialCoach" placeholder="请选择专项教练" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in sysUserList" :key="dict.userId" :label="dict.userName" :value="dict.userId" />
                </el-select>
              </el-form-item>

              <el-form-item label="体能教练" prop="fitnessCoach">
                <el-select v-model="form.fitnessCoach" placeholder="请选择体能教练" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in sysUserList" :key="dict.userId" :label="dict.userName" :value="dict.userId" />
                </el-select>
              </el-form-item>
              <el-form-item label="队医" prop="teamDoctor">
                <el-select v-model="form.teamDoctor" placeholder="请选择队医" clearable size="small" style="width: 240px">
                  <el-option v-for="dict in sysUserList" :key="dict.userId" :label="dict.userName" :value="dict.userId" />
                </el-select>
              </el-form-item>
              <el-form-item label="时间" prop="startTime">
                <el-date-picker clearable size="small" style="width: 200px" v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择开始时间">
                </el-date-picker>
                <el-date-picker clearable size="small" style="width: 200px" v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择结束时间">
                </el-date-picker>
              </el-form-item>
              <!--<el-form-item label="强度" prop="strength">
                                    <el-input v-model="form.strength" placeholder="请输入强度"/>
                                </el-form-item>-->
              <el-form-item label="描述" prop="outline">
                <el-input v-model="form.outline" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </div>
          </div>
        </el-form>
      </el-scrollbar>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTrainingWeek,
  listGetSysUser,
  listGetAnnualPlan,
  getTrainingWeek,
  delTrainingWeek,
  addTrainingWeek,
  updateTrainingWeek,
  exportTrainingWeek,
} from "@/api/training/trainingWeek";

import { listLocation } from "@/api/training/trainingLocation";

export default {
  name: "TrainingWeek",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // trainingWeek表格数据
      trainingWeekList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //训练周期下拉框集合
      annualPlanIdList: [],
      //自然年份下拉框集合
      naturalYearList: [],
      annualPlanList: [],
      //用户名称集合
      sysUserList: [],
      //训练地点集合
      trainingLocationList: [],
      //周型集合
      typeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        annualPlanId: undefined,
        cycleName: undefined,
        startTime: undefined,
        endTime: undefined,
        week: undefined,
        strength: undefined,
        trainingLocationId: undefined,
        teamLeader: undefined,
        headCoach: undefined,
        specialCoach: undefined,
        fitnessCoach: undefined,
        teamDoctor: undefined,
        outline: undefined,
        ctime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        annualPlanId: [
          { required: true, message: "周期名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    "$route.query.annualPlanId": function (val, oldVal) {
      this.queryParams.annualPlanId = val || undefined;
      this.getList();
    },
  },
  created() {
    this.queryParams.annualPlanId = this.$route.query.annualPlanId || undefined;
    this.getList();
    this.getDicts("week_type").then((response) => {
      this.typeList = response.data;
    });
  },
  methods: {
    /** 查询trainingWeek列表 */
    getList() {
      this.loading = true;
      listTrainingWeek(this.queryParams).then((response) => {
        this.trainingWeekList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 周型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeList, row.type);
    },
    //周期名称集合
    getAnnualPlanList() {
      this.loading = true;
      listGetAnnualPlan(this.queryParams).then((response) => {
        this.annualPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //用户名称集合
    getSysUserList() {
      this.loading = true;
      listGetSysUser(this.queryParams).then((response) => {
        this.sysUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //训练地点集合
    getTrainingLocationList() {
      this.loading = true;
      listLocation(this.queryParams).then((response) => {
        this.trainingLocationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        annualPlanId: undefined,
        startTime: undefined,
        endTime: undefined,
        week: undefined,
        strength: undefined,
        trainingLocation: undefined,
        teamLeader: undefined,
        headCoach: undefined,
        specialCoach: undefined,
        fitnessCoach: undefined,
        teamDoctor: undefined,
        outline: undefined,
        ctime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getAnnualPlanList();
      this.getSysUserList();
      this.getTrainingLocationList();

      this.reset();
      this.open = true;
      this.title = "添加训练周";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getAnnualPlanList();
      this.getSysUserList();
      this.getTrainingLocationList();
      this.reset();
      const id = row.id || this.ids;
      getTrainingWeek(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改训练周";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTrainingWeek(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addTrainingWeek(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除训练周编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delTrainingWeek(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有训练周数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportTrainingWeek(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () { });
    },
  },
};
</script>